<template>
  <div>
    <div class="SearchBar">
      <el-row>
        <template v-if="buttons[0] == 0">
          <template v-if="isDel">
            <el-button type="primary" size="mini" @click="del">删除</el-button>
          </template>
        </template>
        <template v-else v-for="(btn,index) in buttons">
          <el-button :type="btn.type" :icon="btn.icon" :key="index" :disabled="btn.disabled" :size="btn.size" @click.native.prevent="btn.method()">{{btn.label}}</el-button>
        </template>
        <el-button size="mini" @click="update" type="primary" icon="el-icon-refresh-right">刷新</el-button>
        <div class="search" v-if="isSearch">
          <el-input placeholder="请输入内容" v-model="content" class="input-with-select">
            <el-button slot="append" icon="el-icon-search" @click="search('search')"></el-button>
          </el-input>
        </div>
      </el-row>
    </div>
  </div>
</template>
<script>
export default {
  name:'SearchBar',
  props:{
    buttons:{type:Array, default:()=>[0]},
    isSearch:{type:Boolean, default:()=>true},
    isDel:{type:Boolean, default:()=>true},
    serachData:{type:Array, default:()=>[]},
    searchKey:{type:String, default:()=>"json"}
  },
  data(){
    return {
      content:""
    }
  },
  created(){
  },
  methods:{
    del(){
      this.$emit("del")
    },
    update(){
      this.$emit("refresh")
    },
    search(val){
      let obj = {}
      if(this.searchKey == "json"){
        if(this.content.indexOf("&") != -1){
          let data = this.content.split("&")
          for(let i of data){
            if(i.split("=").length == 2){
              for(let j in i.split("=")){
                if(this.serachData.indexOf(i.split("=")[j])){
                  obj[i.split("=")[0]] = i.split("=")[1]
                }
              }
            }
          }
        }else{
          if(this.content.indexOf("=") != -1){
            if(this.content.split("=").length==2){
              for(let i in this.content.split("=")){
                if(this.serachData.indexOf(this.content.split("=")[i])){
                  obj[this.content.split("=")[0]] = this.content.split("=")[1]
                }
              }
            }
          }
        }
      }
      obj = this.content
      this.$emit("refresh",obj,val)
    }
  }
}
</script>
<style lang="less">
  .SearchBar{
    position: relative;
    padding: 10px 0px 0px 20px;
    .search{
      .el-input,.el-input__inner{
        height: 30px;
      }
      position: absolute;
      right: 0;
      top: -5px;
    }
  }
</style>